home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / misc_pto / mwpetz09 / poepoem.c < prev    next >
C/C++ Source or Header  |  1991-06-02  |  6KB  |  204 lines

  1. /* POEPOEM.C -- Demonstrates User-Defined Resource */
  2.  
  3. #ifdef MEWEL
  4. #include <window.h>
  5. #undef NULL
  6. #define NULL 0
  7. #else
  8. #include <windows.h>
  9. #endif
  10.  
  11. #include "poepoem.h"
  12.  
  13. long FAR PASCAL WndProc  (HWND, unsigned, WORD, LONG) ;
  14.  
  15. char   szAppName [10] ;
  16. char   szCaption [35] ;
  17. HANDLE hInst ;
  18.  
  19. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  20.      HANDLE   hInstance, hPrevInstance ;
  21.      LPSTR    lpszCmdLine ;
  22.      int      nCmdShow ;
  23.      {
  24.      HWND     hWnd ;
  25.      MSG      msg ;
  26.      WNDCLASS wndclass ;
  27.  
  28.      if (!hPrevInstance) 
  29.           {
  30.           LoadString (hInstance, IDS_APPNAME, szAppName, sizeof szAppName) ;
  31.           LoadString (hInstance, IDS_CAPTION, szCaption, sizeof szCaption) ;
  32.  
  33.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  34.           wndclass.lpfnWndProc   = WndProc ;
  35.           wndclass.cbClsExtra    = 0 ;
  36.           wndclass.cbWndExtra    = 0 ;
  37.           wndclass.hInstance     = hInstance ;
  38.           wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
  39.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  40.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  41.           wndclass.lpszMenuName  = NULL ;
  42.           wndclass.lpszClassName = szAppName ;
  43.  
  44.           if (!RegisterClass (&wndclass))
  45.                return FALSE ;
  46.           }
  47.      else
  48.           {
  49.           GetInstanceData (hPrevInstance, szAppName, sizeof szAppName) ;
  50.           GetInstanceData (hPrevInstance, szCaption, sizeof szCaption) ;
  51.           }
  52.  
  53.      hInst = hInstance ;
  54.  
  55.      hWnd = CreateWindow (szAppName, szCaption,
  56.                          WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  57.                          CW_USEDEFAULT, 0,
  58.                          CW_USEDEFAULT, 0,
  59.                          NULL, NULL, hInstance, NULL) ;
  60.  
  61.      ShowWindow (hWnd, nCmdShow) ;
  62.      UpdateWindow (hWnd) ;
  63.  
  64.      while (GetMessage (&msg, NULL, 0, 0))
  65.           {
  66.           TranslateMessage (&msg) ;
  67.           DispatchMessage (&msg) ;
  68.           }
  69.      return msg.wParam ;
  70.      }
  71.  
  72. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  73.      HWND          hWnd ;
  74.      unsigned      iMessage ;
  75.      WORD          wParam ;
  76.      LONG          lParam ;
  77.      {
  78.      static HANDLE hResource ;
  79.      static HWND   hScroll ;
  80.      static short  nPosition, xChar, yChar, yClient, nNumLines, xScroll ;
  81.      char          szPoemRes [15] ;
  82.      char far      *lpText ;
  83.      HDC           hDC ;
  84.      PAINTSTRUCT   ps ;
  85.      RECT          rect ;
  86.      TEXTMETRIC    tm ;
  87.  
  88.      switch (iMessage)
  89.           {
  90.           case WM_CREATE:
  91.  
  92.                hDC = GetDC (hWnd) ;
  93.                GetTextMetrics (hDC, &tm) ;
  94.                xChar = tm.tmAveCharWidth ;
  95.                yChar = tm.tmHeight + tm.tmExternalLeading ;
  96.                ReleaseDC (hWnd, hDC) ;
  97.  
  98.                xScroll = GetSystemMetrics (SM_CXVSCROLL) ;
  99.  
  100.                hScroll = CreateWindow ("scrollbar", NULL,
  101.                               WS_CHILD | WS_VISIBLE | SBS_VERT,
  102.                               0, 0, 0, 0,
  103.                               hWnd, 1, hInst, NULL) ;
  104.  
  105.                LoadString (hInst, IDS_POEMRES, szPoemRes, sizeof szPoemRes) ;
  106.                hResource = LoadResource (hInst, 
  107. #ifdef MEWEL
  108.                            FindResource (hInst, szPoemRes, "STEXT")) ;
  109. #else
  110.                            FindResource (hInst, szPoemRes, "TEXT")) ;
  111. #endif
  112.  
  113.                lpText = LockResource (hResource) ;
  114.  
  115.                nNumLines = 0 ;
  116.                while (*lpText != '\0' && *lpText != '\x1A')
  117.                     {
  118.                     if (*lpText == '\n')
  119.                          nNumLines ++ ;
  120.                     lpText = AnsiNext (lpText) ;
  121.                     }
  122.                *lpText = '\0' ;
  123.  
  124.                GlobalUnlock (hResource) ;
  125.  
  126.                SetScrollRange (hScroll, SB_CTL, 0, nNumLines, FALSE) ;
  127.                SetScrollPos   (hScroll, SB_CTL, 0, FALSE) ;
  128.                break ;
  129.  
  130.           case WM_SIZE:
  131.                MoveWindow (hScroll, LOWORD (lParam) - xScroll, 0,
  132.                     xScroll, yClient = HIWORD (lParam), TRUE) ;
  133.                SetFocus (hWnd) ;
  134.                break ; 
  135.  
  136.           case WM_SETFOCUS:
  137.                SetFocus (hScroll) ;
  138.                break ;
  139.  
  140.           case WM_VSCROLL:
  141.                switch (wParam)
  142.                     {
  143.                     case SB_TOP:
  144.                          nPosition = 0 ;
  145.                          break ;
  146.                     case SB_BOTTOM:
  147.                          nPosition = nNumLines ;
  148.                          break ;
  149.                     case SB_LINEUP:
  150.                          nPosition -= 1 ;
  151.                          break ;
  152.                     case SB_LINEDOWN:
  153.                          nPosition += 1 ;
  154.                          break ;
  155.                     case SB_PAGEUP:
  156.                          nPosition -= yClient / yChar ;
  157.                          break ;
  158.                     case SB_PAGEDOWN:
  159.                          nPosition += yClient / yChar ;
  160.                          break ;
  161.                     case SB_THUMBPOSITION:
  162.                          nPosition = LOWORD (lParam) ;
  163.                          break ;
  164.                     }
  165.                nPosition = max (0, min (nPosition, nNumLines)) ;
  166.                if (nPosition != GetScrollPos (hScroll, SB_CTL))
  167.                     {
  168.                     SetScrollPos (hScroll, SB_CTL, nPosition, TRUE) ;
  169.                     InvalidateRect (hWnd, NULL, TRUE) ;
  170.                     }
  171.                break ;
  172.  
  173.           case WM_PAINT:
  174.                hDC = BeginPaint (hWnd, &ps) ;
  175.  
  176.                lpText = LockResource (hResource) ;
  177.  
  178.                GetClientRect (hWnd, &rect) ;
  179.                rect.left += xChar ;
  180.                rect.top  += yChar * (1 - nPosition) ;
  181.                DrawText (hDC, lpText, -1, &rect, DT_EXTERNALLEADING) ;
  182.  
  183.                GlobalUnlock (hResource) ;
  184.  
  185.                EndPaint (hWnd, &ps) ;
  186.                break ;
  187.  
  188.           case WM_DESTROY:
  189.                FreeResource (hResource) ;
  190.                PostQuitMessage (0) ;
  191.                break ;
  192.  
  193.           default:
  194.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  195.           }
  196.      return 0L ;
  197.      }
  198.  
  199. #ifdef MEWEL
  200. MEWELmain(0)
  201. #endif
  202.  
  203.  
  204.